#include<iostream>
using namespace std;
#include<vector>
#include<fstream>

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        int row = grid.size();
        int col = grid[0].size();
        int count = 0;
        for(int i=0; i<row; i++) {
            for(int j=0; j<col; j++) {
                if(grid[i][j] == '1') {
                    infect(grid,i,j);
                    count++;
                }
            }
        }
        return count;
    }

    void infect(vector<vector<char>>& grid,int x,int y)
    {
        int row = grid.size();
        int col = grid[0].size();
        if(x < 0 || x >= row || y < 0 || y >= col || grid[x][y] != '1') {
            return;
        }
        if(grid[x][y] == '1') {
            grid[x][y] = '2';
        }
        //这里也可以把下面四个递归调用放到上面的if里面 会加快代码执行
        infect(grid,x+1,y); //下
        infect(grid,x-1,y); //上
        infect(grid,x,y+1); //右
        infect(grid,x,y-1); //左
    }
};


int main() {
}